package com.im.partchat.topic.manager;

import android.os.Handler;
import android.os.Message;

import com.im.partchat.App;
import com.im.partchat.topic.bean.PraiseResponse;
import com.im.partchat.topic.bean.TopicBean;
import com.im.partchat.topic.tools.HttpUtil;
import com.im.partchat.topic.tools.JsonParser;

import java.util.ArrayList;
import java.util.HashMap;


public class HomeManager {


	private final static int TOPIC_DELECT = 0x07;
	private final static int TOPIC_DELECT_FAIL = 0x08;
	private final static int TOPIC_Praise = 0x10;

	private ArrayList<TopicDeleteListener> onTopicDeleteListenerList; 
	private ArrayList<TopicPraiseCountChangeListener> onTopicPraiseCountChangeListenerList;
	 
	private JsonParser jp = new JsonParser();
	private App mApp;

	public HomeManager(App mApp) {
		this.mApp = mApp;
		this.onTopicDeleteListenerList = new ArrayList<TopicDeleteListener>();
		this.onTopicPraiseCountChangeListenerList = new ArrayList<TopicPraiseCountChangeListener>();
	}

	private Handler handler = new Handler() {
		@SuppressWarnings("unchecked")
		public void handleMessage(Message msg) {
			switch (msg.what) {
			
			case TOPIC_DELECT:
				for (TopicDeleteListener onTopicDeleteListener : onTopicDeleteListenerList) {
					onTopicDeleteListener.onTopicDeleteFinish(msg.arg1, true);
				}
				break;
			case TOPIC_DELECT_FAIL:
				for (TopicDeleteListener onTopicDeleteListener : onTopicDeleteListenerList) {
					onTopicDeleteListener.onTopicDeleteFinish(msg.arg1, false);
				}
				break;
			
			case TOPIC_Praise:
				PraiseResponse praiseResult = (PraiseResponse) msg.obj;
				if (!praiseResult.isSuccess()) {
					//点赞网络失败
					for (TopicPraiseCountChangeListener onTopicPraiseCountChangeListener : onTopicPraiseCountChangeListenerList) {
						onTopicPraiseCountChangeListener.onTopicPraiseCountChanged(praiseResult);
					}
				}
				
				break;
			
			}
		}
	};



	/**
	 * 处理的比较好的 点赞取消赞效果（微信微博） 是不弹出dialog的但是带动画，但是点赞需要通知服务器同步
	 * 
	 * 先立刻调用动画效果，然后本地直接模拟一个网络成功返回数据，再用观察者模式刷新所有activity包含该动态的点赞状态
	 * 然后发出网络请求
	 * 如果网络请求成功，什么都不做
	 * 如果网络失败，状态弹回之前的，再用观察者模式刷新所有activity包含该动态的点赞状态
	 * @param bean
	 * @param uid
	 */
	public void startPraiseTopic(TopicBean bean, String uid) {
		PraiseResponse response  =  new PraiseResponse();
		response.setCode(1);
		response.setTopicid(bean.getTopicId());
		if (bean.isPraised()) {//之前是点过的
			response.setStillPraise(false);
			response.setPraiseCnt(bean.getPraiseCount()-1);
		} else {
			response.setStillPraise(true);
			response.setPraiseCnt(bean.getPraiseCount()+1);
		}
		
		for (TopicPraiseCountChangeListener onTopicPraiseCountChangeListener : onTopicPraiseCountChangeListenerList) {
			onTopicPraiseCountChangeListener.onTopicPraiseCountChanged(response);
		}
		new Thread(new PraiseTopicRun(response, uid)).start();
	}

	private class PraiseTopicRun implements Runnable {
		private PraiseResponse preResponse;
		private String uid;
		public PraiseTopicRun(PraiseResponse preResponse,  String uid) {
			this.preResponse = preResponse;
			this.uid = uid;
		}

		@SuppressWarnings("unchecked")
		public void run() {
			// TODO Auto-generated method stub
			PraiseResponse response = null;
			try {
				HashMap<String, String> data = new HashMap<String, String>();
				data.put("userid", uid);
				data.put("topicid", ""+preResponse.getTopicid());
				response = jp.getPraiseResponse(HttpUtil.postMsg(
						HttpUtil.getData(data), HttpUtil.IP + "topic/praise"));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (response == null) {
				response = new PraiseResponse();
				response.setMessage("网络访问失败");
			}
			
			response.setTopicid(preResponse.getTopicid());
			if (!response.isSuccess()) {
				//	 * 如果网络失败，状态弹回之前的，再用观察者模式刷新该动态的点赞状态
				response.setStillPraise(!preResponse.isStillPraise());
				response.setPraiseCnt(preResponse.isStillPraise()?preResponse.getPraiseCnt()-1:preResponse.getPraiseCnt()+1);
			}

			handler.sendMessage(handler.obtainMessage(TOPIC_Praise, response));
		}
	};

	
	
	


	/**
	 * 话题删除 start
	 * 
	 * @author acer
	 * 
	 */
	public interface TopicDeleteListener {
		public void onTopicDeleteFinish(int topicID, boolean success);
	}

	public void addOnTopicDeleteListener(
			TopicDeleteListener onTopicDeleteListener) {
		if (!onTopicDeleteListenerList.contains(onTopicDeleteListener)) {
			onTopicDeleteListenerList.add(onTopicDeleteListener);
		}
	}

	public void removeOnTopicDeleteListener(
			TopicDeleteListener onTopicDeleteListener) {
		if (onTopicDeleteListener != null
				&& onTopicDeleteListenerList.contains(onTopicDeleteListener)) {
			this.onTopicDeleteListenerList.remove(onTopicDeleteListener);
		}
	}

	/**
	 * 话题删除 end
	 * 
	 * @author acer
	 */

	/*********************************************************************************/

	
	/**
	 * 赞 start
	 * 
	 * @author acer
	 * 
	 */
	public interface TopicPraiseCountChangeListener {
		public void onTopicPraiseCountChanged(PraiseResponse regResult);
	}

	public void addOnTopicPraiseCountChangeListener(
			TopicPraiseCountChangeListener onTopicPraiseCountChangeListener) {
		if (!onTopicPraiseCountChangeListenerList
				.contains(onTopicPraiseCountChangeListener)) {
			this.onTopicPraiseCountChangeListenerList
					.add(onTopicPraiseCountChangeListener);
		}
	}

	public void removeOnTopicPraiseCountChangeListener(
			TopicPraiseCountChangeListener onTopicPraiseCountChangeListener) {
		if (onTopicPraiseCountChangeListener != null
				&& onTopicPraiseCountChangeListenerList
						.contains(onTopicPraiseCountChangeListener)) {
			this.onTopicPraiseCountChangeListenerList
					.remove(onTopicPraiseCountChangeListener);
		}
	}

	/**
	 * 赞 end
	 * 
	 * @author acer
	 * 
	 */
	/*********************************************************************************/
}
